package main

import "fmt"

//借用字符串:4ms 32%,2MB 20%
func reverse(x int) int {
	min, max := -1<<31, 1<<31-1
	res := 0
	s := fmt.Sprint(x)
	if x < 0 {
		for i := len(s) - 1; i > 0; i-- {
			res = res*10 + int(s[i]-'0')
			if res*-1 < min {
				return 0
			}
		}
		res *= -1
	} else {
		for i := len(s) - 1; i >= 0; i-- {
			res = res*10 + int(s[i]-'0')
			if res > max {
				return 0
			}
		}
	}
	return res
}

//纯数学操作:0ms 100%,2MB 68%
func reverse1(x int) int {
	res := 0
	for x != 0 {
		c := x % 10
		x /= 10
		res = res*10 + c
		if res > 1<<31-1 || res < -1<<31 {
			return 0
		}
	}
	return res
}
func main() {
	fmt.Println(reverse(-154))
	fmt.Println(reverse1(123))
	fmt.Println(-105%10, -2%10, -105/10)
}
